package com.jzhao.gper.interpreter;

public class OptUtil {

    /**
     * 判断是否是运算符
     * @param element
     * @return
     */
    public static boolean isSymbol(String element) {
        return element.equals("+") || element.equals("-") || element.equals("*") || element.equals("/");
    }

    /**
     * 判断是否是运算符
     * @param element
     * @return
     */
    public static boolean isBracket(String element) {
        return element.equals("(") ;
    }

    /**
     * 先不考虑括号嵌套
     * @param i
     * @param elements
     * @return
     */
    public static String[] getElements(int i,String[] elements){
        int lastBracket=0;
        for(int j=(elements.length-1);j>=i;j--){
            if(elements[j].equals(")")){
                lastBracket = j;
                break;
            }
        }
        String[] results = new String[lastBracket-i+1];
        int index=0;
        for(int j=i;j<=lastBracket;j++){
            results[index++] = elements[j];
        }

        return results;
    }

    public static ICalculationExpression createOperationExpression(String element, ICalculationExpression left, ICalculationExpression right) {
        if(element.equals("+")){
            return new AddExpression(left,right);
        }else if(element.equals("-")){
            return new SubExpression(left,right);
        }else if(element.equals("*")){
            return new MultiExpression(left,right);
        }else{
            return new SubExpression(left,right);
        }
    }
}
